<?php
namespace app\common\controller;

/**
 * 我的logic类
 * Trait MyLogic
 * @package app\common\controller
 */
trait TyLogic {

    /**
     * 返回信息
     * @param $code
     * @param $msg
     * @param string $data
     * @param string $url
     * @return mixed
     */
    public function get_response($code,$msg,$data='',$url=''){
        $result['code'] = $code;
        $result['msg'] = $msg;
        $result['data'] = $data;
        $result['url'] = $url;

        return $result;
    }

    /**
     * 获取应用配置信息
     * @param array $maps
     * @return array
     */
    public function get_conf_info($maps=[]){
        $conf_info = model('conf')->get_list($maps);
        $conf_info_new = [];
        foreach ($conf_info as $key => $value) {
            $conf_info_new[$value['key']] = $value['value'];
        }

        return $conf_info_new;
    }

    /**
     * 修改应用配置信息
     * @param $data
     * @return int
     */
    public function save_conf_info($data){
        $r = false;
        foreach ($data as $k => $v){
            $c_data = [];
            $c_data['value'] = $v;

            $maps = [];
            $maps[] = ['key','=',$k];
            $r = model('conf')->where($maps)->update($c_data);
        }
        if($r !== false){
            return 1;
        }
        return 0;
    }

    /**
     * 检查用户规则
     * @param string $field
     * @return mixed
     */
    public function check_user_rule($field='*'){
        $maps = [];
        $maps[] = ['openid','=',session('openid')];
        $maps[] = ['role','>',1];
        $detail = model('user')->get_detail($maps,$field);
        if(empty($detail)){
            $data['detail'] = '';
            $data['is_register'] = 0;
            return $this->get_response(2,'您尚未注册，请立即注册',$data);
        }
        if($detail['enable'] == 0){
            return $this->get_response(0,'该用户已禁用');
        }

        //获取用户权限
        $maps = [];
        $maps[] = ['openid','=',session('openid')];
        $up_detail = model('user_power')->get_detail($maps,'id,user_power');
        $detail['user_power'] = !empty($up_detail['user_power']) ? json_decode($up_detail['user_power']) : [];

        //获取角色权限
        $maps = [];
        $maps[] = ['id','=',$detail['role']];
        $r_detail = model('role')->get_detail($maps,'id,mini_ws_detail');

        $role_power = [];
        $role_power['mini_ws_detail'] = !empty($r_detail['mini_ws_detail']) ? json_decode($r_detail['mini_ws_detail'],true) : [];
        $detail['role_power'] = $role_power;

        if($detail['status'] != 1){
            $data['detail'] = $detail;
            $data['is_register'] = 1;
            return $this->get_response(2,'该用户审核中或已驳回',$data);
        }

        $data['detail'] = $detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 检查管理员规则
     * @param string $field
     * @return mixed
     */
    public function check_admin_rule($field='*'){
        $maps = [];
        $maps[] = ['openid','=',session('admin_openid')];
        $detail = model('user')->get_detail($maps,$field);
        if(empty($detail)){
            return $this->get_response(2,'您尚未注册，是否立即注册');
        }
        if($detail['status'] != 1){
            return $this->get_response(2,'该用户审核中或已驳回');
        }
        if($detail['enable'] == 0){
            return $this->get_response(0,'该用户已禁用');
        }

        $data['detail'] = $detail;
        return $this->get_response(1,'OK',$data);
    }
}
